#!/bin/bash
# DJVU_Image_Print_1.sh ( 4 ) : This script , by Frank Trampe , spools the monochrome representation of each page in a djvu file to CUPS in direct image form so as to avert the stretching so harmful to halftones that sometimes happens when employing other methods .
# This script takes up to six arguments : the file that needs to be printed , the number of copies , the destination printer , the starting page , the ending page , and the word keep if one wishes to keep the temporary page files or any other word otherwise .
# If only the file is specified , the script prints all pages in the file to the default printer . In absence of a specifying argument , the starting page is assumed to be the first page , the ending page is assumed to be the last , the number of copies is assumed to be one , the printer is assumed to be the default , and all of the temporary files are deleted .

Page_Count_1=`djvudump $1 | awk '{ count = 0 } /pages/ { for(i=2;i<=NF;i++){ if($i ~ "pages"){b=i-1; print $b}}}'` ;
if [ $4 ] ; then
Start1=$4 ;
else
Start1=1 ;
fi
if [ $5 ] ; then
End1=$5 ;
else
End1=$Page_Count_1 ;
fi 
if [ $2 ] ; then
Copies1=$2 ;
else
Copies1=1 ;
fi
for PNum1 in `seq $Start1 $End1` ; do
ddjvu -mode=black -format=tiff -page=$PNum1 $1 Page_`Number_Crop_1 4 $PNum1`.tiff ;
if [ $3 ] ; then
lpr -o natural-scaling=100 -o ppi=600 -o top-left -o page-left=0 -o page-right=0 -o page-top=0 -o page-bottom=0 -# $Copies1 -P $3 Page_`Number_Crop_1 4 $PNum1`.tiff ;
else
lpr -o natural-scaling=100 -o ppi=600 -o top-left -o page-left=0 -o page-right=0 -o page-top=0 -o page-bottom=0  -# $Copies1 Page_`Number_Crop_1 4 $PNum1`.tiff ;
fi
done
if [ $4 = "save" ] ; then
echo 'Page_*.tiff' ;
else
rm ./Page_*.tiff ;
fi

